<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/11
 * Time: 9:41
 */
namespace app\admin\controller;
use think\Db;
class Customs  extends Base {
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            $where['rs_del'] = 1;
            //更新时间
            if(isset($params['coupon_start']) && $params['coupon_start'] != '' && isset($params['coupon_end']) && $params['coupon_end'] != ''){
                $where['rs_update_time'] = ['between', [$params['coupon_start'], $params['coupon_end']]];
            }
            if(isset($params['coupon_start']) && $params['coupon_start'] != '' && isset($params['coupon_end']) && $params['coupon_end'] == ''){
                $where['rs_update_time'] = ['>=', $params['coupon_start']];
            }if(isset($params['coupon_start']) && $params['coupon_start'] == '' && isset($params['coupon_end']) && $params['coupon_end'] != ''){
                $where['rs_update_time'] = ['<=', $params['coupon_end']];
            }
            //关卡
            if(isset($params['rg_id']) && $params['rg_id'] != ''){
                $where['rs_game_id'] = $params['rg_id'];
            }
            //关键字
            if(isset($params['keyword']) && $params['keyword'] != ''){
                $keyword = $params['keyword'];
                $where['a.rs_id|a.rs_name|urb.rb_name'] = ['like', '%' . $keyword . '%'];
            }
            //使用状态
            if(isset($params['rs_status']) && $params['rs_status'] != ''){
                $where['rs_status'] = $params['rs_status'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('reading_subject')->alias('a')
                ->join('up_admin ua','ua.id = a.rs_admin')
                ->join('up_reading_book urb', 'urb.rb_id = a.rs_book_id', 'left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('reading_subject')->alias('a')
                ->join('up_admin ua','ua.id = a.rs_admin')
                ->join('up_reading_book urb', 'urb.rb_id = a.rs_book_id', 'left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field('a.*,ua.username,urb.rb_name')
                ->order('rs_id desc')
                ->select();
            foreach ($lists as $key=>&$vo){
//                $rb_name = Db::table('up_reading_book')->where('rb_id',$vo['rs_book_id'])->value('rb_name');
//                $rg_name = Db::table('up_reading_game')->where('rg_rb_id',$vo['rs_game_id'])->value('rg_name');
//                $vo['rb_name'] = $rb_name;
//                $vo['rg_name'] = $rg_name;
                if($vo['rs_type']==1){
                    $vo['rs_type_name'] = '认读能力';
                }elseif ($vo['rs_type']==2){
                    $vo['rs_type_name'] = '记诵能力';
                }elseif ($vo['rs_type']==3){
                    $vo['rs_type_name'] = '理解能力';
                }elseif ($vo['rs_type']==4){
                    $vo['rs_type_name'] = '鉴赏能力';
                }elseif ($vo['rs_type']==5){
                    $vo['rs_type_name'] = '活用能力';
                }

            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        //关卡
        $reading_game = Db::table('up_reading_game')->field('rg_id, rg_name')->select();
        $this->assign('reading_game', $reading_game);
        return $this->fetch();
    }
    /**
     * 批量修改状态
     * @return \think\response\Json
     */
    public function couponStatus(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择试题']);
        }
        $status = Db::table('up_reading_subject')->where('rs_id', 'IN', $ids)->column('rs_status');
        if($num == 1){//上架
            if(in_array(1, $status)){
                return json(['code' => 100, 'msg' => '所选试题中含有上架中试题，请重新选择']);
            }
            $msg = '上架成功';
        }else{//下架
            if(in_array(2, $status)){
                return json(['code' => 100, 'msg' => '所选试题中含有下架中试题，请重新选择']);
            }
            $msg = '下架成功';
        }
        $res = Db::table('up_reading_subject')->where('rs_id', 'IN', $ids)->setField('rs_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => $msg]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 导入Excel
     * @return mixed
     */
    public function excelIn(){
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
//            print_r($highestRow);die;
            $time = date('Y-m-d H:i:s');
                for ($i=2;$i<=$highestRow;$i++) {
                    $a = $objPHPExcel->getActiveSheet ()->getCell ("A" . $i)->getValue ();//获取A列的值
                    $b = $objPHPExcel->getActiveSheet ()->getCell ("B" . $i)->getValue ();//获取B列的值
                    $c = $objPHPExcel->getActiveSheet ()->getCell ("C" . $i)->getValue ();//获取C列的值
                    $d = $objPHPExcel->getActiveSheet ()->getCell ("D" . $i)->getValue ();//获取D列的值
                    $e = $objPHPExcel->getActiveSheet ()->getCell ("E" . $i)->getValue ();//获取E列的值
                    $f = $objPHPExcel->getActiveSheet ()->getCell ("F" . $i)->getValue ();//获取F列的值
                    $g = $objPHPExcel->getActiveSheet ()->getCell ("G" . $i)->getValue ();//获取F列的值
                    $h = $objPHPExcel->getActiveSheet ()->getCell ("H" . $i)->getValue ();//获取F列的值
                    $i2 = $objPHPExcel->getActiveSheet ()->getCell("I" . $i)->getValue ();//获取F列的值
                    $j2 = $objPHPExcel->getActiveSheet ()->getCell("J" . $i)->getValue ();//获取F列的值
                    if($a != '' && $b != '' && $c != '' && $d != '' && $e != '' && $f != '' && $g != '' && $h != '' && $i2 != '' && $j2 != ''){
                        //插入学校
                        $data = [
                            'rs_name' => trim($a, ' '),//学校名称
                            'rs_a' => trim($b, ' '),
                            'rs_b' => trim($c, ' '),
                            'rs_c' => trim($d, ' '),//省
                            'rs_d' => trim($e, ' '),//市
                            'rs_answer' => trim($f, ' '),//区
                            'rs_game_id' => trim($g, ' '),//区
                            'rs_book_id' => trim($h, ' '),//区
                            'rs_remark' => trim($i2, ' '),//区
                            'rs_difficult' => trim($j2, ' '),//区
                            'rs_time' => $time,//区
                            'rs_update_time' => $time,//区
                            'rs_admin' => session('uid'),//区
                        ];
                        Db::table('up_reading_subject')->insertGetId($data);
                    }
                }
            echo '/admin/customs/index';
        }
        return $this->fetch('customs/excelin');
    }

    public function update(){
        if (request()->isPost()){
            $id = input('param.id');
            $aa = input('param.rs_name');
            $a = input('param.rs_a');
            $b = input('param.rs_b');
            $c = input('param.rs_c');
            $d = input('param.rs_d');
            $e = input('param.rs_answer');
            $f = input('param.rs_difficult');
            $g = input('param.rs_remark');
            $rs_type = input('param.rs_type');
            $time = date('Y-m-d H:i:s',time());
            $data = [
                'rs_name' => $aa,//学校名称
                'rs_a' => $a,
                'rs_b' => $b,
                'rs_c' => $c,//省
                'rs_d' => $d,//市
                'rs_answer' => $e,//区
                'rs_remark' => $g,//区
                'rs_difficult' => $f,//区
                'rs_update_time' => $time,//区
                'rs_type' => $rs_type,//区
                'rs_admin' => session('uid'),//区
            ];
            $res= Db::table('up_reading_subject')->where('rs_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => "操作成功"]);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.id');
        $info = Db::table('up_reading_subject')->where('rs_id',$id)->find();
        $info2 = Db::table('up_reading_game')->select();
        $info1 = Db::table('up_reading_book')->select();
        $this->assign('info',$info);
        $this->assign('info1',$info1);
        $this->assign('info2',$info2);
        return $this->fetch();
    }


    public function del(){
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择试题']);
        }
        $status = Db::table('up_reading_subject')->where('rs_id', 'IN', $ids)->column('rs_status');
        if(in_array(1, $status)){
            return json(['code' => 100, 'msg' => '所选试题中含有上架中试题，请重新选择']);
        }

        $res = Db::table('up_reading_subject')->where('rs_id', 'IN', $ids)->setField('rs_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}